package org.gnyxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.gnyxy.constant.DeleteConstant;
import org.gnyxy.constant.MessageConstant;
import org.gnyxy.constant.RecommendedConstant;
import org.gnyxy.constant.StatusConstant;
import org.gnyxy.dto.MaterialDTO;
import org.gnyxy.dto.MaterialPageQueryDTO;
import org.gnyxy.entity.DishInfo;
import org.gnyxy.entity.Material;
import org.gnyxy.exception.BaseException;
import org.gnyxy.mapper.DishInfoMapper;
import org.gnyxy.mapper.MaterialMapper;
import org.gnyxy.result.PageResult;
import org.gnyxy.service.MaterialService;
import org.gnyxy.vo.MaterialUserVO;
import org.gnyxy.vo.MaterialVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class MaterialServiceImpl implements MaterialService {
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private DishInfoMapper dishInfoMapper;

    /**
     * 新增菜品材料
     * @param materialDTO
     */
    @Override
    public void add(MaterialDTO materialDTO) {
        // 校验是否为空
        if (materialDTO == null){
            throw new BaseException(MessageConstant.MATERIAL_IS_NULL);
        }
        if (materialDTO.getRecommended() == null){
            materialDTO.setRecommended(RecommendedConstant.DISABLE);
        }
        // 设置不删除
        materialDTO.setDeleted(DeleteConstant.IS_NOT_DELETED);
        // 如果不传入状态 默认为禁用
        if (materialDTO.getStatus() == null){
            materialDTO.setStatus(StatusConstant.DISABLE);
        }
        materialMapper.insert(materialDTO);
    }

    /**
     * 分页查询
     * @param materialPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(MaterialPageQueryDTO materialPageQueryDTO) {
        // 查询页数和页面 limit
        PageHelper.startPage(materialPageQueryDTO.getPage(), materialPageQueryDTO.getPageSize());
        Page<MaterialVO> materialVOS = materialMapper.pageQuery(materialPageQueryDTO);
        return new PageResult(materialVOS.getTotal(),materialVOS.getResult());
    }

    /**
     * 更新
     * @param materialDTO
     */
    @Override
    public void update(MaterialDTO materialDTO) {
        // 校验是否为空
        if (materialDTO == null){
            throw new BaseException(MessageConstant.MATERIAL_IS_NULL);
        }
        if (materialDTO.getRecommended() == null){
            materialDTO.setRecommended(RecommendedConstant.DISABLE);
        }
        materialMapper.update(materialDTO);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public MaterialVO getById(Long id) {
        // 校验id不为空
        if (id == null){
            throw new BaseException(MessageConstant.ID_IS_NULL);
        }
        return materialMapper.getById(id);
    }

    /**
     * 根据id删除菜品原材料
     * @param id
     */
    @Override
    public void deletebyId(Long id) {
        // 校验id不为空
        if (id == null){
            throw new BaseException(MessageConstant.ID_IS_NULL);
        }
        // 查询信息
        MaterialVO materialVO = getById(id);
        // 创建并拷贝对象等
        MaterialDTO material = new MaterialDTO();
        BeanUtils.copyProperties(materialVO,material);
        // 设置删除
        material.setDeleted(DeleteConstant.IS_DELETED);
        // 更新
        materialMapper.update(material);
    }

    /**
     * 设置状态
     * @param status
     */
    @Override
    public void setStatus(Integer status,Long id) {
        // 查询信息
        MaterialVO materialVO = getById(id);
        if (materialVO == null){
            throw new BaseException(MessageConstant.MATERIAL_IS_NULL);
        }
        // 设置状态
        materialVO.setStatus(status);
        // 创建并拷贝对象等
        MaterialDTO materialDTO = new MaterialDTO();
        BeanUtils.copyProperties(materialVO,materialDTO);
        // 更新
        materialMapper.update(materialDTO);
    }

    /**
     * 查看未被禁用的菜品材料
     * @return
     */
    @Override
    public List<MaterialVO> getTrueStatus(String name) {
        List<Material> materials = materialMapper.getTrueStatus(name);
        return materials.stream().map(material -> {
            MaterialVO materialVO = new MaterialVO();
            BeanUtils.copyProperties(material, materialVO);
            return materialVO;
        }).collect(Collectors.toList());
    }

    /**
     * 获取所有菜品原材料(配菜)
     * @return
     */
    @Override
    public List<MaterialUserVO> getAll() {
        List<Material> materials = materialMapper.getAll();
        return materials.stream().map(material -> {
            MaterialUserVO materialUserVO = new MaterialUserVO();
            BeanUtils.copyProperties(material,materialUserVO);
            return materialUserVO;
        }).collect(Collectors.toList());
    }

    @Override
    public MaterialUserVO getTureStatusById(Long id) {
        Material material = materialMapper.getStatusTrueById(id);
        MaterialUserVO materialUserVO = new MaterialUserVO();
        BeanUtils.copyProperties(material,materialUserVO);
        return materialUserVO;
    }

    @Override
    public List<MaterialUserVO> getTrueStatusByName(String name) {
        List<Material> materials = materialMapper.getTrueStatus(name);
        List<MaterialUserVO> materialUserVOS = new ArrayList<>();
        for (Material material : materials) {
            MaterialUserVO materialUserVO = new MaterialUserVO();
            BeanUtils.copyProperties(material,materialUserVO);
            materialUserVOS.add(materialUserVO);
        }
        return materialUserVOS;
    }
}
